package demo.soban.com.myapplication

import android.graphics.Color
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.Gravity
import android.view.View
import android.widget.EditText
import android.widget.LinearLayout
import org.jetbrains.anko.*

class MainActivity : AppCompatActivity() {

    lateinit var et_account: EditText
    lateinit var et_password: EditText

    companion object {
        val BIN_ID = 1
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_main)
        UI().setContentView(this@MainActivity)
    }

    inner class UI : AnkoComponent<MainActivity> {
        override fun createView(ui: AnkoContext<MainActivity>): View {
            return with(ui) {
                verticalLayout {
                    padding = 40
                    backgroundColor = context.resources.getColor(android.R.color.white)
                    gravity = Gravity.CENTER_HORIZONTAL
                    simpleTitle().lparams {
                        height = 100
                        width = matchParent
                    }
                    imageView(R.mipmap.ic_launcher).lparams {
                        width = dip(80)
                        height = dip(80)
                        topMargin = dip(40)
                    }
                    linearLayout {
                        gravity = Gravity.CENTER_VERTICAL
                        orientation = LinearLayout.HORIZONTAL
                        imageView(R.mipmap.ic_launcher).lparams(width = wrapContent, height = wrapContent)
                        et_account = editText {
                            id = BIN_ID
                            hint = "登录账户"
                            hintTextColor = Color.parseColor("#666666")
                            textSize = 16f
                            lines = 1
                            background = null
                        }
                    }
                    linearLayout {
                        gravity = Gravity.CENTER_VERTICAL
                        orientation = LinearLayout.HORIZONTAL
                        imageView(R.mipmap.ic_launcher).lparams(width = wrapContent, height = wrapContent)
                        et_password = editText {
                            hint = "账户密码"
                            hintTextColor = Color.parseColor("#666666")
                            textSize = 16f
                            background = null
                        }
                    }
                    button("登录") {
                        onClick {
                            if (et_account.text.toString().isNotEmpty() && et_password.text.toString().isNotEmpty())
                                startActivity<MainActivity>("account" to et_account.text.toString(), "password" to et_password.text.toString())
                            else toast("请输入账户或者密码")

                            this.text = "aaaa"
                            this.text = "aaaa"
                            this.text = "aaaa"
                            this.text = "aaaa"

                        }
                    }.lparams(width = matchParent, height = wrapContent) {
                        topMargin = 20
                        bottomMargin = 20
                    }
                    relativeLayout {
                        textView("记住密码") {
                        }.lparams {
                            alignParentLeft()
                        }
                        textView("隐私协议") {
                        }.lparams {
                            alignParentRight()
                        }
                    }
                }
            }
        }
    }
}
